home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / nihcl-30.lha / nihcl-3.0 / lib / Rectangle.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  3KB  |  158 lines

  1. /* Rectangle.c -- implementation of class Rectangle
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet: kgorlen@alw.nih.gov
  20.     September, 1985
  21.  
  22. Function:
  23.     
  24. A Rectangle is defined by two points: an origin, which specifies the top
  25. left corner, and corner, which specifies the bottom right corner.
  26.  
  27. $Log:    Rectangle.c,v $
  28.  * Revision 3.0  90/05/20  00:20:58  kgorlen
  29.  * Release for 1st edition.
  30.  * 
  31. */
  32.  
  33. #include "Rectangle.h"
  34. #include "nihclIO.h"
  35.  
  36. #define    THIS    Rectangle
  37. #define    BASE    Object
  38. #define BASE_CLASSES BASE::desc()
  39. #define MEMBER_CLASSES Point::desc(),Point::desc()
  40. #define VIRTUAL_BASE_CLASSES Object::desc()
  41.  
  42. DEFINE_CLASS(Rectangle,2,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/Rectangle.c,v 3.0 90/05/20 00:20:58 kgorlen Rel $",NULL,NULL);
  43.  
  44. Rectangle::Rectangle(int left, int top, int height, int width)
  45. {
  46.     tl = Point(left,top);
  47.     br = Point(left+width,top+height);
  48. }
  49.  
  50. Rectangle::Rectangle(const Point& o, const Point& c)
  51. {
  52.     tl = o;
  53.     br = c;
  54. }
  55.  
  56. Rectangle::Rectangle(const Rectangle& r) : tl(r.tl), br(r.br) {}
  57.  
  58. bool Rectangle::operator==(const Rectangle& r) const
  59. {
  60.     return (tl==r.tl && br==r.br);
  61. }
  62.  
  63. Rectangle Rectangle::operator&&(const Rectangle& r) const
  64. {
  65.     return Rectangle(tl.max(r.tl), br.min(r.br));
  66. }
  67.  
  68. Rectangle Rectangle::operator||(const Rectangle& r) const
  69. {
  70.     return Rectangle(tl.min(r.tl),
  71.         br.max(r.br));
  72. }
  73.  
  74. void Rectangle::operator+=(const Point& p)
  75. {
  76.     tl += p;
  77.     br += p;
  78. }
  79.  
  80. void Rectangle::operator-=(const Point& p)
  81. {
  82.     tl -= p;
  83.     br -= p;
  84. }
  85.  
  86. bool Rectangle::contains(const Point& p) const
  87. {
  88.     return (tl <= p) && (p <= br);
  89. }
  90.  
  91. bool Rectangle::contains(const Rectangle& r) const
  92. {
  93.     return (contains(r.tl) && contains(r.br));
  94. }
  95.  
  96. bool Rectangle::intersects(const Rectangle& r) const
  97. {
  98.     if (tl.max(r.tl) < br.min(r.br)) return YES;
  99.     return NO;
  100. }
  101.  
  102. void Rectangle::moveTo(const Point& p)
  103. /*
  104.     Move this Rectangle so its origin is at p.
  105. */
  106. {
  107.     br += p-tl;
  108.     tl = p;
  109. }
  110.  
  111. void Rectangle::deepenShallowCopy()    {}
  112.  
  113. unsigned Rectangle::hash() const    { return tl.hash()^br.hash(); }
  114.  
  115. bool Rectangle::isEqual(const Object& r) const
  116. {
  117.     return r.isSpecies(classDesc) && *this==castdown(r);
  118. }
  119.  
  120. const Class* Rectangle::species() const { return &classDesc; }
  121.  
  122. void Rectangle::printOn(ostream& strm) const
  123. {
  124.     strm << tl << " corner: " << br;
  125. }
  126.  
  127. Rectangle::Rectangle(OIOin& strm)
  128.     : BASE(strm),
  129.     tl(strm), br(strm)
  130. {
  131. }
  132.  
  133. void Rectangle::storer(OIOout& strm) const
  134. {
  135.     BASE::storer(strm);
  136.     tl.storeMemberOn(strm);
  137.     br.storeMemberOn(strm);
  138. }
  139.  
  140. Rectangle::Rectangle(OIOifd& fd)
  141.     : BASE(fd),
  142.     tl(fd), br(fd)
  143. {
  144. }
  145.  
  146. void Rectangle::storer(OIOofd& fd) const 
  147. {
  148.     BASE::storer(fd);
  149.         tl.storeMemberOn(fd);
  150.     br.storeMemberOn(fd);
  151. }
  152.  
  153. int Rectangle::compare(const Object&) const
  154. {
  155.     shouldNotImplement("compare");
  156.     return 0;
  157. }
  158.